package com.ktjiaoyu.crm;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import javax.annotation.Resource;

@SpringBootTest
public class UserControllerTester {

    @Resource
    private WebApplicationContext wac;  //注入Spring Web上下文环境
    private static MockMvc mockMvc;

    /**
     * 在测试启动时需要提前加载的内容，这里是提前记载MVC环境
     */
    @BeforeEach
    public void setUp()throws Exception{
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }

    @Test
    public  void testGetUser() throws Exception{
        String mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/user/2"))
                .andReturn().getResponse().getContentAsString();//返回相应主体内容
        System.out.println(mvcResult);
    }
}
